package project2011;
import java.io.*;
/**
 * Point of sale manages interactions between GUI and 
 * user, seller, product lists
 * @author Antone Gonzalez
 */
public class POS {

    /**
     * A constructor for POS initiates userList and sellerList
     */
    public POS()
    {
        userList = new UserList();
        sellerList = new SellerList();
        inventory = new ProductList();
        bank = new Bank();
        
        
        loadProductlist();
        loadSellerlist();
        loadUserlist();
        loadBank();

    }

    /**
     * Checks passed in userName and password against database of Users 
     * @param userName name of user to be found
     * @param password entered password
     * @return true if user is found and password is correct
     */
    public boolean checkUserCredentials(String userName, String password)
    {
        User temp = userList.findUser(userName);
        if (temp != null)
        {
            if(password.equals(temp.getPassword()))
            {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Checks passed in userName and password against database of Sellers
     * @param userName name of seller to be found
     * @param password entered password
     * @return true if user is found and password is correct
     */
    public boolean checkSellerCredentials(String userName, String password)
    {
        Seller temp = sellerList.findSeller(userName);
        if (temp != null)
        {
            if(password.equals(temp.getPassword()))
            {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Checks credentials passed in against the user and seller databases
     * @param userName the userName to search for
     * @param password the password to search for
     */
    public int login(String userName, String password)
    {
        boolean isUser = checkUserCredentials(userName, password);
        boolean isSeller = checkSellerCredentials(userName, password);

        if(isUser)
        {
            return 1;
        }
        else if(isSeller)
        {           
            return 2;
        }
        else
        {
            return 0;
        }     
    }

    /**
     * adds passed in product to seller inventory
     * @param toAdd the product to be add
     * @param seller the seller to add the product to
     */
    public void addProduct(Product toAdd, Seller seller)
    {
        seller.addProduct(toAdd);
        inventory.addProduct(toAdd);
    }
    
    /**
     * returns a string containing the printout of all products in an inventory
     * @return a string containing the printout of all products in an inventory
     */
    public String printAllProducts()
    {
        return inventory.print();
    }
    
    /**
     * finds and returns a seller with a passed in name
     * @param name the name of the seller to find
     * @return the seller with the passed in name
     */
    public Seller findSeller(String name)
    {
        return sellerList.findSeller(name);
    }
    
    /**
     * finds and returns a user with a passed in name
     * @param name the name of the user to find
     * @return the user with the passed in name
     */
    public User findUser(String name)
    {
        return userList.findUser(name);
    }
    
    /**
     * Writes the users to txt file Userlist
     */
    public void saveUserlist()
    {
        try
        {
            FileWriter outFile = new FileWriter("Userlist.txt");
            PrintWriter out = new PrintWriter(outFile);
            
            out.print(userList.print());
            out.close();
        } 
        
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * Loads users from txt file Userlist
     */
    public final void loadUserlist()
    {
        try 
        {
            String patternStr = ","; 
            BufferedReader in = new BufferedReader(new FileReader("Userlist.txt"));
            String str;
            while ((str = in.readLine()) != null) 
            {
                String[] fields = str.split(patternStr);
                String name = fields[0];
                String password = fields[1];
                User toAdd = new User(name, password);
                userList.addUser(toAdd);
            }
            in.close();
        }               
        catch (IOException e) 
        {
            
        } 
    }
    /**
     * Saves products to txt file Productlist
     */
    public void saveProductlist()
    {
        try
        {
            FileWriter outFile = new FileWriter("Productlist.txt");
            PrintWriter out = new PrintWriter(outFile);
            
            out.print(inventory.print());
            out.close();
        } 
        
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * loads a product list from a txt file into memory
     */
    public final void loadProductlist()
    {
        try 
        {
            String patternStr = ","; 
            BufferedReader in = new BufferedReader(new FileReader("Productlist.txt"));
            String str;
            while ((str = in.readLine()) != null) 
            {
                String[] fields = str.split(patternStr);
                int upc = Integer.parseInt(fields[0]);
                String productName = fields[1];
                double unitPrice = Double.parseDouble(fields[2]);
                String vendorName = fields[3];
                Product toAdd = new Product(upc, productName, unitPrice, vendorName);
                inventory.addProduct(toAdd);
            }
            in.close();
        }
                
        catch (IOException e) 
        {
            
        } 
    }
    
    /**
     * saves the seller list to a txt file named Sellerlist.txt
     */
    public void saveSellerlist()
    {
        try
        {
            FileWriter outFile = new FileWriter("Sellerlist.txt");
            PrintWriter out = new PrintWriter(outFile);

            out.print(sellerList.print());
            out.close();
        } 

        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * louds a seller list from a txt file to memory
     */
    public final void loadSellerlist()
    {
        try 
        {
            String patternStr = ","; 
            BufferedReader in = new BufferedReader(new FileReader("Sellerlist.txt"));
            String str;
            while ((str = in.readLine()) != null) 
            {
                String[] fields = str.split(patternStr);
                String name = fields[0];
                String password = fields[1];
                int bankaccount = Integer.parseInt(fields[2]);
                int numOfProducts = Integer.parseInt(fields[3]);
                Seller toAdd = new Seller(name, password, bankaccount);
                sellerList.addSeller(toAdd);
                if(numOfProducts != 0)
                {
                   
                    for(int i = 0; i < numOfProducts; i++)
                    {
                        str = in.readLine();
                        String[] productFields = str.split(patternStr);
                        int UPC = Integer.parseInt(productFields[0]);
                        String productName = productFields[1];
                        double unitPrice = Double.parseDouble(productFields[2]);
                        String vendorName = productFields[3];
                        Product temp = new Product(UPC, productName, unitPrice, vendorName);
                        toAdd.addProduct(temp);
                    }
                }
            }
            in.close();
        }
                
        catch (IOException e) 
        {
            
        } 
    }
    
    /**
     * saves the content of the bank into a txt file
     */
    public void saveBank(){
        try
        {
            FileWriter outFile = new FileWriter("BankAccountlist.txt");
            PrintWriter out = new PrintWriter(outFile);
            
            out.print(bank.print());
            out.close();
        } 
        
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    
    /**
     * loads a bank from a txt file to memory
     */
    public final void loadBank(){
     try 
        {
            String patternStr = ","; 
            BufferedReader in = new BufferedReader(new FileReader("BankAccountlist.txt"));
            String str;
            while ((str = in.readLine()) != null) 
            {
                String[] fields = str.split(patternStr);
                int accountNumber = Integer.parseInt(fields[0]);
                double balance = Double.parseDouble(fields[1]);
                //System.out.println("load-> account#" + fields[0] + " balance" + fields[1]);
                bankAccount toAdd = new bankAccount(accountNumber, balance);
                bank.add(toAdd);
            }
            in.close();
        }
                
        catch (IOException e) 
        {
            
        }    
    }
        
    private UserList userList;
    private SellerList sellerList;
    protected Bank bank;
    protected ProductList inventory;
}
